﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.IO;
using System.Text;
using System.Windows.Forms;

using AtletIS.Shared.Entities;
using AtletIS.Shared.Data.aDbDataSetTableAdapters;

namespace AtletIS.Shared.Helpers
{
    public static class ChipSmsHelper
    {
        public static ChipDataTableAdapter taChipdata = new ChipDataTableAdapter();
        public static CheckPointsTableAdapter taCheckpoints = new CheckPointsTableAdapter();

        /// <summary>
        ///  Vygeneruje CSV pre SMS bránu
        /// </summary>
        public static bool CreateCsv(DisciplineListEntity discipline, bool bChipDateTime, bool bCleanChipDateTime)
        {
            string path = "Ext\\Chip\\sms_" + discipline.ID.ToString() + ".csv";
            StreamWriter sw = new StreamWriter(path, false, Encoding.UTF8);

            try
            {
                Cursor.Current = Cursors.WaitCursor;

                // Nacita cielovy check-point
                DataTable dtFinishCheckpoint = DatabaseHelper.GetCheckPointsByDisciplineListIdFinish(discipline.ID);
                if (dtFinishCheckpoint.Rows.Count == 0)
                {
                    sw.WriteLine("Disciplína nemá žiadne check-pointy (" + discipline.Name.ToString() + ").");
                    return false;
                }

                // Get chip data
                DataTable dtChipData = DatabaseHelper.GetChipDataByCheckPointId(Convert.ToInt32(dtFinishCheckpoint.Rows[0]["ID"])); 

                if (dtChipData.Rows.Count > 0)
                {
                    foreach (DataRow drChipData in dtChipData.Rows)
                    {
                        // Get time
                        DateTime chipDateTime = (drChipData["ChipDateTime"] != DBNull.Value) ? Convert.ToDateTime(drChipData["ChipDateTime"]) : new DateTime();
                        DateTime chipDateTimeClean = ((drChipData["CleanChipDateTime"]) != DBNull.Value) ? Convert.ToDateTime(drChipData["CleanChipDateTime"]) : new DateTime();

                        // Round time
                        chipDateTime = RoundingHelper.RoundUpDateTime(chipDateTime, discipline.ResultCode);
                        chipDateTimeClean = RoundingHelper.RoundUpDateTime(chipDateTimeClean, discipline.ResultCode);
                        
                        string smsMessage = string.Empty;
                        if (drChipData["SMS"].ToString().Trim() != string.Empty)
                        {
                            smsMessage = drChipData["SMS"].ToString() + ";";
                            smsMessage += "Meno: " + PrintHelper.PrintName(drChipData) + " ";
                            smsMessage += "Disciplina: " + discipline.Name.ToString() + " ";
                            
                            if (bChipDateTime)
                            {
                                smsMessage += "Cas: ";
                                smsMessage += PrintHelper.PrintResult2(chipDateTime, discipline.ResultCode) + " ";
                            }
                            
                            if (bCleanChipDateTime)
                            {
                                smsMessage += "Cisty cas: ";
                                smsMessage += PrintHelper.PrintResult2(chipDateTimeClean, discipline.ResultCode);
                            }

                            sw.Write(smsMessage + Environment.NewLine);
                        }
                    }
                }

                MessageBox.Show("Súbor s SMS bol vygenerovaný." + Environment.NewLine + path, "SMS", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(discipline.Name + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace, "J-Chip results", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            finally
            {
                Cursor.Current = Cursors.Default;
                sw.Close();
                sw.Dispose();
            }

        }
    
    
    
    
    }
}
